//
//  AppsViewController.m
//  reSearchDemo
//
//  Created by 云库 on 15/9/22.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "AppsViewController.h"
#import "UIView+Category.h"
#import "UIButton+Category.h"
#import "GoodsSortVC.h"
#import "MoreSortVC.h"

#define MARGIN 8
#define HEIGHT self.view.height/5
#define WIDTH self.view.width

@interface AppsViewController ()

/** 按钮frame*/
@property (strong, nonatomic) NSArray *btnFrameArray;
/** 滚动视图*/
@property (strong, nonatomic) UIScrollView *scrollView;

/** typeId数组*/
@property (strong, nonatomic) NSArray *typeIdArr;
/** 分类名称*/
@property (strong, nonatomic) NSArray *sortNameArr;

@end



@implementation AppsViewController



- (void)viewWillAppear:(BOOL)animated{
    self.typeIdArr = @[@32,@82,@83,@37,@34,@84,@35,@85,@40,@45];
    self.sortNameArr = @[@"服装服饰",@"护肤彩妆",@"皮具鞋类",@"家纺家饰",@"运动户外",@"日用百货",@"珠宝配饰",@"手机数码",@"社区服务",@"食品",@"更多团购"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.scrollView];
    for (int i = 0; i < self.btnFrameArray.count; i++) {
        UIButton *btn = [UIButton buttonWithTarget:self action:@selector(btnClick:) image:[NSString stringWithFormat:@"分类%d",i+1] ];
        btn.tag = i;
        //btn.titleLabel.text = self.typeIdArr[6];
        CGRect rect = [self.btnFrameArray[i] CGRectValue];
        btn.frame = rect;
        [self.scrollView addSubview:btn];
    }
    
    
}



-  (void)btnClick:(UIButton *)button{
    
    if (button.tag==10) {
        DLog(@"点击了更多分类");
        MoreSortVC *sortVc = [[MoreSortVC alloc] init];
        [self pushViewController:sortVc];
    }else{
        //下一界面
    GoodsSortVC *sort = [[GoodsSortVC alloc] init];
    sort.title = self.sortNameArr[button.tag];
    sort.typeId = self.typeIdArr[button.tag];
    [self pushViewController:sort];
    
    }
}
- (NSArray *)btnFrameArray{
    if (!_btnFrameArray) {
        
        CGRect rect1 = CGRectMake(MARGIN, MARGIN, (WIDTH-3*MARGIN)/2, (WIDTH)/2);
        CGRect rect2 = CGRectMake(rect1.size.width+2*MARGIN, MARGIN, (WIDTH-3*MARGIN)/2, (WIDTH)/2);
        CGRect rect3 = CGRectMake(MARGIN, rect2.size.height+2*MARGIN, (WIDTH-4*MARGIN)/3, (WIDTH-4*MARGIN)/3);
        CGRect rect4 = CGRectMake(rect3.size.width+2*MARGIN, rect2.size.height+2*MARGIN, (WIDTH-4*MARGIN)/3, (WIDTH-4*MARGIN)/3);
        CGRect rect5 = CGRectMake(rect4.origin.x+rect4.size.width+MARGIN, rect2.size.height+2*MARGIN, (WIDTH-4*MARGIN)/3, (WIDTH-4*MARGIN)/3);
        CGRect rect6 = CGRectMake(MARGIN, rect5.origin.y+rect5.size.height+MARGIN, (WIDTH-3*MARGIN)/2, (WIDTH-3*MARGIN)/2);
        CGRect rect7 = CGRectMake(rect6.size.width+2*MARGIN, rect5.origin.y+rect5.size.height+MARGIN, (WIDTH-3*MARGIN)/2, (WIDTH-3*MARGIN)/2);
        CGRect rect8 = CGRectMake(MARGIN, rect7.origin.y+rect7.size.height+MARGIN, (WIDTH)/3+2*MARGIN, (WIDTH-3*MARGIN)/2);
        CGRect rect9 = CGRectMake(rect8.size.width+2*MARGIN, rect7.origin.y+rect7.size.height+MARGIN, (WIDTH-3*MARGIN-rect8.size.width), (WIDTH-3*MARGIN)/2);
        CGRect rect10 = CGRectMake(MARGIN, rect9.origin.y+rect9.size.height+MARGIN, (WIDTH-3*MARGIN)/2, (WIDTH-3*MARGIN)/2);
        CGRect rect11 = CGRectMake(rect10.size.width+2*MARGIN, rect9.origin.y+rect9.size.height+MARGIN, (WIDTH-3*MARGIN)/2, (WIDTH-3*MARGIN)/2);
        
        
        NSValue *value1 = [NSValue valueWithCGRect:rect1];
        NSValue *value2 = [NSValue valueWithCGRect:rect2];
        NSValue *value3 = [NSValue valueWithCGRect:rect3];
        NSValue *value4 = [NSValue valueWithCGRect:rect4];
        NSValue *value5 = [NSValue valueWithCGRect:rect5];
        NSValue *value6 = [NSValue valueWithCGRect:rect6];
        NSValue *value7 = [NSValue valueWithCGRect:rect7];
        NSValue *value8 = [NSValue valueWithCGRect:rect8];
        NSValue *value9 = [NSValue valueWithCGRect:rect9];
        NSValue *value10 = [NSValue valueWithCGRect:rect10];
        NSValue *value11 = [NSValue valueWithCGRect:rect11];
        
        /** 滚动视图内容高度*/
        self.scrollView.contentSize = CGSizeMake(self.view.width, rect11.origin.y+rect11.size.height+55+64);
        
        _btnFrameArray = @[value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11];
    }
    return _btnFrameArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshDataListIfNeed{
    
    
}
@end
